package com.whfc.ms.api.cfg.service.factory;

import com.alibaba.fastjson.JSON;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.fuum.dto.open.OpenApiLicenseDTO;
import com.whfc.fuum.param.open.OpenApiRuleEditParam;
import com.whfc.fuum.service.OpenApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-07-30 16:52
 */
@Service(ConfigCenterServiceConstant.OPEN_SERVICE)
public class ConfigCenterServiceOpenImpl implements ConfigCenterService {

    @DubboReference(interfaceClass = OpenApiService.class, version = "1.0.0")
    private OpenApiService openApiService;

    @Override
    public List getConfigList(Integer deptId, String module) {
        if (ConfigCenterModuleConstant.OPEN_LICENSE.equals(module)) {
            return openApiService.getOpenApiLicenseList(deptId);
        } else if (ConfigCenterModuleConstant.OPEN_LICENSE_RULE.equals(module)) {
            return openApiService.getOpenApiRulesByDeptId(deptId);
        }
        return Collections.emptyList();
    }

    @Override
    public void setConfig(Integer deptId, String module, String dataStr) {
        if (ConfigCenterModuleConstant.OPEN_LICENSE.equals(module)) {
            List<OpenApiLicenseDTO> openApiLicenseList = JSON.parseArray(dataStr, OpenApiLicenseDTO.class);
            if (openApiLicenseList == null || openApiLicenseList.isEmpty()) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "参数类型错误.");
            }
            openApiService.configureOpenApiLicense(deptId, openApiLicenseList);
        } else if (ConfigCenterModuleConstant.OPEN_LICENSE_RULE.equals(module)) {
            List<Integer> ruleIdList = JSON.parseArray(dataStr, Integer.class);
            if (ruleIdList == null) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "参数类型错误.");
            }
            OpenApiRuleEditParam ruleEditParam = new OpenApiRuleEditParam();
            ruleEditParam.setDeptId(deptId);
            ruleEditParam.setRuleIdList(ruleIdList);
            openApiService.editOpenApiRules(ruleEditParam);
        }
    }

}
